<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

function otx_string_unserialize($s)
{
	if (empty($s)) {
		return array();
	}

	if (substr($s, 0, 2) == 'a:') {
		$result = unserialize($s);
		if (false === $result) {
			$result = array();
		}
	} else {
		$result = array();
		$tokens = preg_split('/[\r\n]+/', $s);

		foreach ($tokens as $token) {
			if (false !== strpos($token, '=')) {
				list($key, $value) = explode('=', $token, 2);
				$result[$key] = $value;
			} else
				$result[] = $token;
		}
	}

	return $result;
}

function otx_string_random($length)
{
	$characters = str_shuffle('ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789');
	$random_str = '';

	mt_srand((double) microtime() * 1000000);
	while (strlen($random_str) < $length) {
		$random_str .= $characters{mt_rand(0, strlen($characters) - 1)};
	}

	return $random_str;
}

function otx_string_uuid()
{
	return md5(uniqid(mt_rand(), true));
}

function otx_string_crypt($s)
{
	return md5($s . otx_config_string('system_crypt_salt'));
}

function otx_string_dump($var)
{
	return print_r($var, true);
}

function otx_string_parse($pattern, $args = array())
{
	foreach ((array) $args as $key => $value) {
		$pattern = str_replace('{' . $key . '}', $value, $pattern);
	}
	return $pattern;
}

function otx_string_search($s, $start, $end)
{
	$start_pos = strpos($s, $start);
	if (false === $start_pos) {
		return false;
	}

	$end_pos = strpos($s, $end);
	if (false === $end_pos) {
		return false;
	}

	return substr($s, $start_pos + strlen($start), $end_pos - $start_pos - strlen($start));
}

function otx_string_trim($s, $left, $right)
{
	if (is_array($left)) {
		foreach ($left as $v) {
			$s = otx_string_trim($s, $v, null);
		}
	} else if (strlen($left) > 0 && strpos($s, $left) === 0) {
		$s = substr($s, strlen($left));
	}

	if (is_array($right)) {
		foreach ($right as $v) {
			$s = otx_string_trim($s, null, $v);
		}
	} else if (strlen($right) > 0 && strpos($s, $right) === strlen($s) - strlen($right)) {
		$s = substr($s, 0, strlen($s) - strlen($right));
	}

	return $s;
}

function otx_string_format_size($size)
{
	if (!is_numeric($size)) {
		return 'N/A';
	}

	if ($size >= 1208925819614629174706176) {
		// Yottabyte (1024 Zettabytes)
		$size = otx_string_format_number(round(($size / 1208925819614629174706176), 2)) . ' ' . otx_language_parse('size_yb');
	} else if ($size >= 1180591620717411303424) {
		// Zetabyte (1024 Exabytes)
		$size = otx_string_format_number(round(($size / 1180591620717411303424), 2)) . ' ' . otx_language_parse('size_zb');
	} else if ($size >= 1152921504606846976) {
		// Exabyte (1024 Petabytes)
		$size = otx_string_format_number(round(($size / 1152921504606846976), 2)) . ' ' . otx_language_parse('size_eb');
	} else if ($size >= 1125899906842624) {
		// Petabyte (1024 Terabytes)
		$size = otx_string_format_number(round(($size / 1125899906842624), 2)) . ' ' . otx_language_parse('size_pb');
	} else if ($size >= 1099511627776) {
		// Terabyte (1024 Gigabytes)
		$size = otx_string_format_number(round(($size / 1099511627776), 2)) . ' ' . otx_language_parse('size_tb');
	} else if ($size >= 1073741824) {
		// Gigabyte (1024 Megabytes)
		$size = otx_string_format_number(round(($size / 1073741824), 2)) . ' ' . otx_language_parse('size_gb');
	} else if ($size >= 1048576) {
		// Megabyte (1024 Kilobytes)
		$size = otx_string_format_number(round(($size / 1048576), 2)) . ' ' . otx_language_parse('size_mb');
	} else if ($size >= 1024) {
		// Kilobyte (1024 bytes)
		$size = otx_string_format_number(round(($size / 1024), 2)) . ' ' . otx_language_parse('size_kb');
	} else if ($size == 0) {
		$size = '0 ' . otx_language_parse('size_bytes');
	} else {
		$size = otx_string_format_number($size) . ' ' . otx_language_parse('size_bytes');
	}

	return $size;
}

function otx_string_format_number($number)
{
	if ($number == "-") {
		return $number;
	}

	if (is_int($number)) {
		return number_format($number, 0, otx_language_parse('decimal_point'), otx_language_parse('thousand_point'));
	} else {
		$parts = explode('.', $number);

		if (isset($parts[1])) {
			$decimals = strlen($parts[1]);
		} else {
			$decimals = 0;
		}

		return number_format((double)$number, $decimals, otx_language_parse('decimal_point'), otx_language_parse('thousand_point'));
	}
}

function otx_string_format_date($ts = null)
{
	return otx_string_local_time($ts, otx_config_string('system_date_format'));
}

function otx_string_format_datetime($ts = null)
{
	return otx_string_local_time($ts, otx_config_string('system_datetime_format'));
}

function otx_string_format_time($ts = null)
{
	return otx_string_local_time($ts, otx_config_string('system_time_format'));
}

function otx_string_local_time($ts = null, $format = null)
{
	if (null === $ts) {
		$ts = $GLOBALS['_XTIMESTAMP'];
	}

	if (null === $format) {
		$format = otx_config_string('system_datetime_format');
	}

	return date($format, $ts);
}